package com.anjie.powerproject.service.impl;

import com.anjie.powerproject.entity.*;

import com.anjie.powerproject.pojo.dto.GraphChangeLogDTO;
import com.anjie.powerproject.repository.GraphChangeLogRepository;
import com.anjie.powerproject.repository.GraphInfoRepository;
import com.anjie.powerproject.service.GraphChangeLogService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Transactional
@Service
public class GraphChangeLogServiceImpl implements GraphChangeLogService {
    @Autowired
    GraphChangeLogRepository graphChangeLogRepository;
    @Autowired
    private GraphInfoRepository graphInfoRepository;

    @Override
    public List<GraphChangeLogDTO> getAll() {
        List<GraphChangeLog> graphChangeLogs=graphChangeLogRepository.findAll();
        List<GraphChangeLogDTO> graphChangeLogDTOS=new ArrayList<>();

        for (GraphChangeLog graphChangelog:graphChangeLogs) {
            GraphChangeLogDTO graphChangeLogDTO=new GraphChangeLogDTO();
            graphChangeLogDTO.setId(graphChangelog.getId());
            graphChangeLogDTO.setLastGraphId(graphChangelog.getLastGraphId());
            graphChangeLogDTO.setCurGraphId(graphChangelog.getCurGraphId());
            graphChangeLogDTO.setUpdateTime(graphChangelog.getUpdateTime());
            GraphInfo lastGraphInfo=graphInfoRepository.getOne(Integer.valueOf(graphChangelog.getLastGraphId()));
            graphChangeLogDTO.setLastGraphName(lastGraphInfo.getGraphName());
            GraphInfo curGraphInfo=graphInfoRepository.getOne(Integer.valueOf(graphChangelog.getCurGraphId()));
            graphChangeLogDTO.setCurGraphName(curGraphInfo.getGraphName());

            graphChangeLogDTOS.add(graphChangeLogDTO);
        }

        return graphChangeLogDTOS;
    }

    @Override
    @Transactional
    public Integer deleteById(Integer id) {
        graphChangeLogRepository.deleteGraphChangeLogById(id);
        return 1;
    }

    @Override
    public GraphChangeLog saveAndFlush(GraphChangeLog graphChangeLog) {
        GraphChangeLog graphLog=graphChangeLogRepository.saveAndFlush(graphChangeLog);
        if(null!=graphLog)
            return graphLog;
        else return null;
    }

    @Override
    public GraphChangeLog getGraphChangeLogById(Integer id){
        GraphChangeLog graphLog=graphChangeLogRepository.getOne(id);
        if(null!=graphLog)
            return graphLog;
        else return null;
    }

}
